<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Agentpay
 *
 * @author wupeng
 */

include 'ApiAbstract.php';
include_once 'Rechange.php';

class Agentpay extends ApiAbstract {
    //put your code here
    
    protected $_paybill;
    
    protected $_tradeNo;


    public function __construct($trade_no, $paybill, $periodStart, $account) {
        $this->_tradeNo = $trade_no;
        $this->_paybill = $paybill;
        
        parent::__construct($periodStart, $account);
        $this->_fields = array(
            'transaction_number' => $trade_no,
            'trader' => 'backend-account-dr',
            'customer_id' => $paybill->AccountId,
            'member' => 'admin',
            'amount' => $paybill->Money,
            'currency' => 'CNY',
            'rate' => '1',
            'rate_amount' => '0',
            'period_start' => $periodStart,
            'period_end' => $account->Money,
            'business_type' => 'spjz-dr',
            'order_sn' => ''
        );
    }
    
    protected function _getTrades() {
        return array(
            Application_Model_PayBill::PAY_PAYPAL => 'other_Paypal',
            Application_Model_PayBill::PAY_ALIPAY => 'other_alipay',
            Application_Model_PayBill::PAY_CARD => 'xinyongka',
            Application_Model_PayBill::PAY_ACCOUNT => 'backend-account-dr'
        );
        
    }
    
    protected function _addRechangeInfo() {
        $rechange = new Rechange($this->_tradeNo, $this->_paybill, $this->_periodStart, $this->_account);
        $rechange->addTransaction();
        $account = Application_Model_UserAccount::load($this->_account->id);
        $this->_periodStart = $account->Money;
        return $this;
    }
    
    public function addTransaction() {
        $trades = $this->_getTrades();
        $this->setFields('trader',$trades[$this->_paybill->payId]);
        $periodStart = $this->_periodStart;
        if ($this->_paybill->payId != Application_Model_PayBill::PAY_ACCOUNT) {
            $this->_addRechangeInfo();
            $periodStart = $this->_account->Money;
        }
        $agentPay = Application_Model_AgentPay::load($this->_paybill->DomainId);
        if($agentPay){
            $user = Application_Model_User::load($this->_paybill->AccountId);
            $this->setFields('business_type', 'spjz-dr')
                ->setFields('amount',$agentPay->payMoney)
                ->setFields('period_start',$periodStart)
                ->setFields('customer_name',$user->nikename)
                ->setFields('period_end',$periodStart - $agentPay->payMoney);
            $url = $this->_buildUrl('addTransaction');
            $this->_updateMageFinance($url, $this->getFields());
            $periodStart -= $agentPay->payMoney;
            $this->_periodStart = $periodStart;
            $this->_addServiceFee($url,$agentPay->processFee);
        }
        return $this;
    }

    protected function _addServiceFee($url,$fee){
        $this->setFields('amount',$fee)
                ->setFields('period_start',$this->_periodStart)
                ->setFields('period_end',$this->_periodStart - $fee)
                ->setFields('business_type','fuwufei-dr');
        
        $this->_updateMageFinance($url, $this->getFields());
    }

}
